package com.service.sys;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.excel.EasyExcel;
import com.common.StringUtils;
import com.common.easyui.EasyuiUtils;
import com.common.easyui.JsonTreeData;
import com.common.http.HttpResult;
import com.common.http.HttpStatus;
import com.dao.sys.MmTypeDao;
import com.model.sys.MmTypeModel;
import com.model.sys.SysDict;
import com.common.model.Global;

@Service
public class MmTypeService {
	static final Logger logger = LoggerFactory.getLogger(MmTypeService.class);

	@Autowired
	MmTypeDao mmTypeDao;

	public List<JsonTreeData> selectTreeList(Map<String, Object> queryMap) {
		HttpResult httpResult = new HttpResult();
		EasyuiUtils.addPageNum(queryMap);
		EasyuiUtils.addFilter(queryMap);
		List<MmTypeModel> list = mmTypeDao.selectList(queryMap);
		int count = mmTypeDao.selectListCount(queryMap);
		List<JsonTreeData> treeDataList = new ArrayList<JsonTreeData>();

		for (MmTypeModel sysDict : list) {
			JsonTreeData treeData = new JsonTreeData();
			treeData.setId(sysDict.getMitId());
			treeData.setPid(sysDict.getMitParentId());
			treeData.setText(sysDict.getMitName());
			treeDataList.add(treeData);
		}
		List<JsonTreeData> newTreeDataList = EasyuiUtils.getTreeJson(treeDataList, "0", "");

		httpResult.setRows(newTreeDataList);
		httpResult.setTotal(count);
		logger.info("selectList:{}", list);
		return newTreeDataList;
	}

	/**
	 * 列表查询
	 *
	 * @param queryMap
	 * @return
	 */
	public HttpResult selectList(Map<String, Object> queryMap) {
		HttpResult httpResult = new HttpResult();
		EasyuiUtils.addPageNum(queryMap);
		EasyuiUtils.addFilter(queryMap);
		List<MmTypeModel> list = mmTypeDao.selectList(queryMap);
		int count = mmTypeDao.selectListCount(queryMap);
		httpResult.setRows(list);
		httpResult.setTotal(count);
		logger.info("selectList:{}", list);
		return httpResult;
	}

	/**
	 * 详情
	 * 
	 * @param
	 * @return
	 */
	public HttpResult selectInfor(String mitId) {
		HttpResult httpResult = new HttpResult();
		MmTypeModel mmTypeModel = mmTypeDao.load(mitId);
		httpResult.setData(mmTypeModel);
		httpResult.setCode(HttpStatus.SUCCESS_CODE);
		return httpResult;
	}

	/**
	 * 保存
	 *
	 * @param
	 * @return
	 */
	public HttpResult save(MmTypeModel mmTypeModel) {
		if (mmTypeModel == null) {
			return HttpResult.error("参数不能为空");
		}
		HttpResult httpResult = new HttpResult();
		int result = 0;
		if (!StringUtils.isNotNull(mmTypeModel.getMitId())) {
			mmTypeModel.setMitId(UUID.randomUUID().toString());
			result = mmTypeDao.insert(mmTypeModel);
		} else {
			result = mmTypeDao.update(mmTypeModel);
		}
		if (result > 0) {
			httpResult.setMsg(mmTypeModel.getStatus().equals(Global.YES + "") ? "提交成功!" : "保存成功!");
			httpResult.setData(mmTypeModel);
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg(mmTypeModel.getStatus().equals(Global.YES + "") ? "提交失败!" : "保存失败!");
		}
		return httpResult;
	}

	/**
	 * 删除
	 *
	 * @param
	 * @return
	 */
	public HttpResult delete(String mitId) {
		HttpResult httpResult = new HttpResult();
		if (!StringUtils.isNotNull(mitId)) {
			return HttpResult.error("参数不能为空");
		}
		int result = mmTypeDao.delete(mitId);
		if (result > 0) {
			httpResult.setMsg("删除成功！");
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg("不存在此条数据！");
		}
		return httpResult;
	}

	/**
	 * 导出
	 * 
	 * @param queryMap
	 * @param response
	 */
	public void export(Map<String, Object> queryMap, HttpServletResponse response) {
		List<MmTypeModel> list = mmTypeDao.selectList(queryMap);
		logger.info("export:{}", list);
		try {
			EasyExcel.write(response.getOutputStream(), MmTypeModel.class).sheet("模板").doWrite(list);
		} catch (IOException e) {
			logger.error("导出错误:{}", e.getMessage());
		}

	}
}
